//-----------------Sonic CD Air Bubbler Script----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.Timer
#alias Object.Value1	:	Object.SpawnTimer
#alias Object.Value2	:	Object.BubbleAmount
#alias Object.Value3	:	Object.BubbleType
#alias Object.Value4	:	Object.Visible

// Air Bubbler alises
#alias Object.XPos		:	AirBubble.XPos
#alias Object.Value1	:	AirBubble.Angle
#alias Object.Value2	:	AirBubble.XVelocity
#alias Object.Value3	:	AirBubble.YVelocity
#alias Object.DrawOrder	:	AirBubble.DrawOrder

// Air Bubble Property Values
#alias 2	:	BUBBLE_SMALL
#alias 4	:	BUBBLE_MEDIUM
#alias 5	:	BUBBLE_BREATHABLE


sub ObjectMain
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Water]
			if Object[ArrayPos0].propertyValue == 0
				if object.ypos > object[ArrayPos0].ypos
					Object.Visible = true
				else
					Object.Visible = false
				end if
			end if
		end if
		ArrayPos0++
	loop

	if Object.Visible == true
		Object.Frame++
		Object.Frame %= 48

		if Object.Timer > 0
			Object.Timer--
		else
			Object.Timer = 220
			
			Rand(Object.BubbleAmount, 6)
			Object.BubbleAmount++
			if Object.BubbleAmount > 6
				Object.BubbleAmount = 6
			end if

			Object.SpawnTimer = 0
			Object.BubbleType = 0
		end if

		if Object.SpawnTimer > 0
			Object.SpawnTimer--
		else
			if Object.BubbleAmount > 0
				Object.BubbleAmount--
				Rand(Object.SpawnTimer, 32)
				Rand(TempValue0, 3)
				if TempValue0 == 2
					if Object.BubbleType == 0
						Object.BubbleType = 1
						CreateTempObject(TypeName[Air Bubble], BUBBLE_BREATHABLE, Object.XPos, Object.YPos)
					else
						CreateTempObject(TypeName[Air Bubble], BUBBLE_MEDIUM, Object.XPos, Object.YPos)
					end if
				else
					CreateTempObject(TypeName[Air Bubble], BUBBLE_SMALL, Object.XPos, Object.YPos)
				end if
				Rand(TempValue0, 16)
				TempValue0  -= 8
				TempValue0 <<= 16

				AirBubble[TempObjectPos].XPos 	  += TempValue0
				AirBubble[TempObjectPos].XVelocity = AirBubble[TempObjectPos].XPos
				Rand(AirBubble[TempObjectPos].Angle, 256)
				AirBubble[TempObjectPos].Angle   <<= 1
				AirBubble[TempObjectPos].DrawOrder = 4
				AirBubble[TempObjectPos].YVelocity = -0x8800
			end if
		end if
	end if
end sub


sub ObjectDraw
	if Object.Visible == true
		TempValue0   = Object.Frame
		TempValue0 >>= 4
		DrawSprite(TempValue0)
	end if
end sub


sub ObjectStartup
	LoadSpriteSheet("R4.5/Water.gif")

	SpriteFrame(-8, -14, 16, 16, 205, 69) 	// #0 - Air Bubbler frame 0
	SpriteFrame(-8, -14, 16, 16, 222, 69) 	// #1 - Air Bubbler frame 1
	SpriteFrame(-8, -14, 16, 16, 239, 69) 	// #2 - Air Bubbler frame 2

	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Air Bubbler]
			Object[ArrayPos0].DrawOrder = 4
		end if
		ArrayPos0++
	loop
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("R4.5/Water.gif")

	SpriteFrame(-8, -14, 16, 16, 205, 69)	// #0 - Air Bubbler

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
